﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace DatingSoonMVC2008.Models
{
    public partial class Dictionary
    {
        private System.Collections.Specialized.StringDictionary _stringDico;
        public Dictionary(string language, IFacade model)
        {
            _stringDico = model.CreateDictionary(language);

        }



        public static Dictionary CreateFromCache(string language, IFacade model)
        {
            if (HttpContext.Current == null)
                return new Dictionary(language, model);

            string cacheKey = "Zoo.Dictionary_" + language;
            System.Web.Caching.Cache aspNetCache = HttpContext.Current.Cache;

            if (aspNetCache[cacheKey] == null)
            {
                aspNetCache.Add(
                    cacheKey,
                    new Dictionary(language, model),
                    null,
                    DateTime.Now.AddMinutes(5),
                    System.Web.Caching.Cache.NoSlidingExpiration,
                    System.Web.Caching.CacheItemPriority.Default,
                    null);
            }
            return (Dictionary)aspNetCache[cacheKey];

        }

        public string this[string key]
        {
            get
            {
                return _stringDico[key.ToLower().Trim()];
            }
        }

        public ListItemCollection GetListItemValues(string listName)
        {
            ListItemCollection col = new ListItemCollection();

            if (_stringDico == null || _stringDico.Keys.Count < 1)
                return col;

            foreach (string key in _stringDico.Keys)
            {
                if (listName.Length + 2 > key.Length)
                    continue;

                if (String.Compare(listName, key.Substring(0, listName.Length), true) == 0)
                {
                    string listItemValue = key.Substring(listName.Length + 1, key.Length - listName.Length - 1);
                    string listItemName = _stringDico[key];
                    col.Add(new ListItem(listItemName, listItemValue));
                }
            }
            return col;
        }
    }
}

